package com.sojson.util.socket;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Socket连接池工具类
 * 
 * @author liu
 * @date 2021-04-04
 */
public class SocketPoolUtil {

    public static ConcurrentMap<String, WebSocketInterface> map = new ConcurrentHashMap<>();

    public static void put(String tokenId, WebSocketInterface myWebSocketInterface) {
        map.put(tokenId, myWebSocketInterface);
    }

    public static WebSocketInterface get(String tokenId) {
        return map.get(tokenId);
    }

    public static void remove(String tokenId) {
        map.remove(tokenId);
    }

    public static Collection<WebSocketInterface> getValues() {
        return map.values();
    }

    /**
     * 群发自定义消息
     */
    public static void sendInfo(String message) throws IOException {
        for (Map.Entry<String, WebSocketInterface> entry : map.entrySet()) {
            WebSocketInterface value = entry.getValue();
            try {
                value.sendMsg(1, "成功！", message, "");
            } catch (Exception e) {
            }
        }
    }
}